﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogicLayer;
using DataAccessLayer;

namespace PresentationLayer
{
    public partial class ViewDisbursementHistory : System.Web.UI.Page
    {
        DisbursementHistoryControl dhc = new DisbursementHistoryControl();
        StationeryInventoryEntities1 ent = new StationeryInventoryEntities1();
        //String userName = "COMM_Head";
        String userName;
        //string userName = "Employee1";
        //String userName = "COMM_Emp1";

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                #region Check if user exists and has permission to view this page
                if (Session["UserLoggedIn"] != null)
                {
                    List<string> list = Session["UserLoggedIn"] as List<string>;
                    if (list[0].Equals("validuser"))
                    {
                        userName = list[3];
                    }
                    else
                    {
                        Response.Redirect("/Login.aspx");
                    }
                }
                else
                {
                    Response.Redirect("/Login.aspx");
                }
                #endregion


                if (!IsPostBack)
                {
                    calFromDate.Visible = false;
                    calToDate.Visible = false;

                    Binding();
                }
            }
            catch (Exception ex)
            { 
            
            }
           
        }

        public void Binding()
        {
            dgvDisbursementHistory.DataSource = dhc.getDisbursementList(userName);
            dgvDisbursementHistory.DataBind();
        }

        protected void ImageButton1_Click1(object sender, ImageClickEventArgs e)
        {
            if (calFromDate.Visible == false)
            {
                calFromDate.Visible = true;
            }
            else
            {
                calFromDate.Visible = false;
            }
        }

        protected void calFromDate_SelectionChanged(object sender, EventArgs e)
        {
            txtFromDate.Text = calFromDate.SelectedDate.ToShortDateString();
            calFromDate.Visible = false;
        }

        protected void ImageButton2_Click1(object sender, ImageClickEventArgs e)
        {
            if (calToDate.Visible == false)
            {
                calToDate.Visible = true;
            }
            else
            {
                calToDate.Visible = false;
            }
        }

        protected void calToDate_SelectionChanged(object sender, EventArgs e)
        {
            txtToDate.Text = calToDate.SelectedDate.ToShortDateString();
            calToDate.Visible = false;
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            try
            {
                DateTime fromDate = calFromDate.SelectedDate;
                DateTime toDate = calToDate.SelectedDate;

                dgvDisbursementHistory.DataSource = dhc.getDisbursementListByDate(userName, fromDate, toDate);
                dgvDisbursementHistory.DataBind();
            }
            catch (Exception ex)
            { 
            
            }
            
        }

        protected void dgvDisbursementHistory_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                if (e.CommandName == "Select")
                {

                    int rowIndex = Convert.ToInt32(e.CommandArgument);
                    String colldate = dgvDisbursementHistory.Rows[rowIndex].Cells[1].Text;
                    //
                    String colltime = dgvDisbursementHistory.Rows[rowIndex].Cells[2].Text;
                    DateTime collectionDate = Convert.ToDateTime(colldate);
                    DateTime collectionTime = Convert.ToDateTime(colltime);
                    String collpoint = dgvDisbursementHistory.Rows[rowIndex].Cells[3].Text;
                    String status;
                    //added today
                    String collPointDetID = dgvDisbursementHistory.Rows[rowIndex].Cells[6].Text;
                    Label lblCpDetialID = (Label)dgvDisbursementHistory.Rows[rowIndex].FindControl("lblCpDetialID");
                    Label lblCStatus = (Label)dgvDisbursementHistory.Rows[rowIndex].FindControl("lblCStatus");
                    Label lblDStatus = (Label)dgvDisbursementHistory.Rows[rowIndex].FindControl("lblDStatus");
                    if (lblCStatus.Text == "" && lblDStatus.Text == "" && lblCpDetialID.Text != "")
                    {
                        status = "Pending";
                    }
                    else if (lblCStatus.Text == "" && lblDStatus.Text != "")
                    {
                        status = "Confirmed By Store Clerk";
                    }
                    else
                    {
                        status = "Collected";
                    }

                    int deptreqid = dhc.getDeptReqID(userName, collectionDate, collPointDetID);
                    String username = userName;

                    Response.Redirect("~/ViewDisbursementHistoryDetail.aspx?colldate=" + colldate + "&colltime=" + colltime + "&collpoint=" + collpoint + "&status=" + status + "&deptreqid=" + deptreqid + "&username=" + username + "&collPointDetId=" + collPointDetID);

                }
            }
            catch(Exception ex)
            { 
            
            }
            
        }


        protected void dgvDisbursementHistory_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                for (int i = 0; i < dgvDisbursementHistory.Rows.Count + 2; i++)
                {
                    if (e.Row.RowType == DataControlRowType.DataRow)
                    {
                        e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#f6f6f6'");
                        e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
                        Label lblCStatus = (Label)e.Row.FindControl("lblCStatus");
                        Label lblDStatus = (Label)e.Row.FindControl("lblDStatus");
                        Label lblStatus = (Label)e.Row.FindControl("lblStatus");

                        if (lblCStatus.Text == "" && lblDStatus.Text == "")
                        {
                            lblStatus.Text = "Pending";
                        }
                        else if (lblCStatus.Text == "")
                        {
                            lblStatus.Text = "Confirmed By Store Clerk";
                        }
                        else if (lblCStatus.Text != "" && lblDStatus.Text != "")
                        {
                            lblStatus.Text = "Collected";
                        }
                        else
                        {
                            lblStatus.Text = "Null";
                        }
                    }
                }
            }
            catch (Exception ex)
            {
            }

            
        }
    }
}